package day11.gec.utils;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class JDBCUtil2 {
	
	
	
	static Properties p = new Properties();
	static {
		try(InputStream is = JDBCUtil2.class.getResourceAsStream("/jdbc.properties")){
			p.load(is);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//提供一个获得连接的方法
	public static Connection getConnection() {
		Connection conn = null;
		try {
			conn = DriverManager.getConnection(p.getProperty("url"), p.getProperty("username"),p.getProperty("password"));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
	
	//关闭连接的方法
	public static void closeConn(Connection conn,Statement st ,ResultSet rs) {
		try {
			if(rs != null) {
				rs.close();
			}
			
			if(st != null) {
				st.close();
			}
			
			if(conn != null) {
				conn.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
